package com.bdqn.controller;

import com.bdqn.LayUI.Layui;
import com.bdqn.Service.Emplyee.EmployeeService;
import com.bdqn.Service.Supplier.SupplierService;
import com.bdqn.Service.SysUsers.SysUsersService;
import com.bdqn.entity.Employee;
import com.bdqn.entity.Supplier;
import com.bdqn.entity.SysUsers;
import org.apache.commons.collections.bag.SynchronizedSortedBag;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.List;

@Controller
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private SysUsersService sysUsersService;

    /**
     * 根据id查询供应商详情
     * @return
     */
    @GetMapping("/supplier_message.html")
    public  Supplier  selectSupplierById(){
        Subject subject= SecurityUtils.getSubject();
        Employee employee =(Employee) subject.getSession().getAttribute("employee");//员工
        Supplier supplier = supplierService.selectSupplierById(employee.getId());
        System.out.println(supplier);
        return supplier;
    }

    @GetMapping("/update_Login_Password.html")
    public Employee selectEmployee(){
        Subject subject= SecurityUtils.getSubject();
        Employee employea =(Employee) subject.getSession().getAttribute("employee");//员工 获取当前登录的用户
//        SysUsers user = (SysUsers) subject.getSession().getAttribute("user");
        //获取当前登录的
        Employee employee = employeeService.selectemployeebyid(employea.getUserId());
        return employee;
    }

    /**
     * 查询旧密码操作
     * @param oldpassword
     * @return
     */
    @GetMapping("/oldpassword")
    @ResponseBody
    public int selectEmployeeandpwd(String oldpassword,Model model){
        String password=selectEmployee().getUser().getPassword();
        Md5Hash md5Hash=new Md5Hash(oldpassword,new Md5Hash(oldpassword),9);
        String s = md5Hash.toHex();
        if(password.equals(s)){
            return 1;
        }else{return 0;}
    }
    /**
     * 修改操作
     * @param
     * @return
     */
    @GetMapping("/updatePwdOK")
    @ResponseBody
    public int updateEmployeeandpwd(String password){
        Md5Hash md5Hash=new Md5Hash(password,new Md5Hash(password),9);
        Subject subject= SecurityUtils.getSubject();
        Employee employea =(Employee) subject.getSession().getAttribute("employee");//员工
        int id = employea.getUserId();//获取这位员工的id
      int de=  sysUsersService.updatePwd(id,md5Hash.toHex());
        System.out.println(de);

        return de;
    }

    /**
     * 查询符合条件的供应商
     */
    @ResponseBody
    @RequestMapping("/SelByMat")
    public List<Supplier> SelectByMatId(@RequestParam(value = "ids[]",required = false) String[] ids){
        List<Supplier> suppliers = supplierService.SelectByMer(ids);
        return suppliers;
    }

    /**
     * 查全部
     */
    @RequestMapping("/Sel")
    @ResponseBody
    public Layui<Supplier> SelX(Integer page, Integer limit, Supplier Supplier){
        page = (page-1)*limit;
        List<Supplier> Suppliers = supplierService.Sel(Supplier,page,limit);
        int count = supplierService.getCount(Supplier);
        Layui<Supplier> stockLayui=new Layui<Supplier>();
        stockLayui.setCode(0);
        stockLayui.setData(Suppliers);
        stockLayui.setCount(count);
        return stockLayui;
    }

    /**
     * 根据id查询供应商详情
     * @return
     */
    @RequestMapping("/supById")
    @ResponseBody
    public  Supplier  selectSupplierById(String id){
        Supplier supplier = supplierService.SelByNum(id);
        return supplier;
    }

    @RequestMapping("/findAll")
    @ResponseBody
    public Layui<Supplier> findAll(){
        List<Supplier> suppliers = supplierService.findAll();
        Layui<Supplier> supplierLayui = new Layui<Supplier>();
        supplierLayui.setCode(0);
        supplierLayui.setData(suppliers);
        return supplierLayui;
    }

    /**
     * 添加供应商
     */
    @RequestMapping("/Insert")
    @ResponseBody
    public boolean Insert(Supplier supplier){
        Calendar calendar=Calendar.getInstance();
        supplier.setSupplierNum(""+calendar.get(Calendar.MINUTE)+calendar.get(Calendar.SECOND));
        return supplierService.Insert(supplier);
    }

    /**
     * 添加用户和员工
     */
    @RequestMapping("/User")
    @ResponseBody
    public boolean User(SysUsers sysUsers,Employee employee){
        String password = sysUsers.getPassword();
        sysUsers.setPassword(String.valueOf(new Md5Hash(password,new Md5Hash(password),9)));
        sysUsersService.InsertUser(sysUsers);
        SysUsers byName = sysUsersService.findByName(sysUsers.getLoginId());
        sysUsersService.InsertUser_role(byName.getId());
        employee.setUserId(byName.getId());
        employeeService.Insert(employee);
        int max = supplierService.max();
        Supplier supplier=new Supplier();
        supplier.setUserId(byName.getId());
        supplier.setId(max);
        supplierService.Update(supplier);
        return true;
    }

}
